This is Peter Hemsley's latest code for BCD conversion referred to in Readout Feb 02, in which he said:

I am pleased to find that some readers are making use of my math routines, as Gerard Galvin is (Cheats Shifted, [i]Readout[r] Dec '01). I also see that Gerard realised that binary division uses the same technique as the decimal long division you were taught at school, it really is that simple. The same also applies to binary multiplication.

Going by the book is not always best. For instance, the subtraction method would be better for small numbers as only a few iterations are required. Gavin states that his routine takes 798 cycles to execute, so I spent ten minutes modifying my binary-to-decimal routine to 10 bits. Although not optimised it executes in 480 cycles, which is a saving of some 300 cycles, and the code is probably a lot smaller too:

;Binary to decimal
;Convert 10 bit binary (count0,1) to decimal digits 1 to 4

	processor 16f84
	include p16f84.inc
	radix dec

;Ram equates
count0	equ	0x0C		;lsb
count1	equ	0x0D		;msb
digit1	equ	0x11		;lsd
digit2	equ	0x12
digit3	equ	0x13
digit4	equ	0x14		;msd
bitcnt	equ	0x19
digcnt	equ	0x1A


	org	0		;test code for MPLAB simulator
	movlw	0x03
	movwf	count1
	movlw	0xFF
	movwf	count0
	call	bin2dec
brk1	return


bin2dec	clrf	digit1		;Clear result
	clrf	digit2
	clrf	digit3
	clrf	digit4
	movlw	6		;Shift 6 places right because
	movwf	bitcnt		;we have only 10 bits
adjup	rlf	count0
	rlf	count1
	decfsz	bitcnt
	goto	adjup

	movlw	10		;10 bits to do
	movwf	bitcnt

bitlp	rlf	count0		;Shift msb into carry
	rlf	count1
	movlw	digit1
	movwf	fsr		;Pointer to digits
	movlw	4		;4 digits to do
	movwf	digcnt
adjlp	rlf	indf		;Shift digit 1 bit left
	movlw	-10
	addwf	indf,w		;Check and adjust for decimal overflow
	skpnc
	movwf	indf

	incf	fsr		;Next digit
	decfsz	digcnt
	goto	adjlp
	decfsz	bitcnt		;Next bit
	goto	bitlp
	return

	end
